package com.waiting.https;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/1/21
 **/
public class HttpClientTest {
    public static void main(String[] args) {
        test();
    }

    public static void test() {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.baidu.com");
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = closeableHttpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通过HttpCLient提供的 <URIBuilder>工具类创建URI
     * @return
     */
    public static URI getURl() {
        URI uri = null;
        try {
            uri = new URIBuilder().setScheme("http").setHost("www.baidu.com").build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return uri;
    }

}
